<template>
  <div class="main-menu">
    <!-- logo -->
    <div class="logo">
      <img src="@\assets\css\imgs\favicon.svg" alt="加载出错..." />
      <h2 v-show="!isCollapse">Z&Z后台管理系统</h2>
    </div>
    <!-- menu -->
    <el-menu :default-active="defaultMenuKey" text-color="#b7bdc3" active-text-color="#fff" background-color="#001529"
      @open="handleOpen" @close="handleClose" :collapse="isCollapse">
      <!-- 递归菜单的实现 -->
      <!-- <template v-for="item in menuList" :key="item.id">
          <recursive-menu-item :item="item" />
        </template> -->
      <template v-for="item in menuList" :key="item.id">
        <el-sub-menu :index="item.id">
          <template #title>
            <el-icon>
              <component :is="item.icon.split('-icon-')?.[1]"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <template v-for="child in item.children" :key="child.id">
            <el-menu-item :index="child.id" @click="changePath(child.url)">{{ child.name }}</el-menu-item>
          </template>
        </el-sub-menu>
      </template>
    </el-menu>
  </div>
</template>
<script lang="ts" setup>
import useLoginStore from '@/store/login/login'
import { findMenuByPath } from '@/utils/map-menus'
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
defineProps({
  isCollapse: {
    type: Boolean,
    required: true,
    default: false
  }
})
const loginStore = useLoginStore()
const menuList = loginStore.userMenu
const route = useRouter()

const routeInfo = useRoute()
const defaultMenuKey = computed(() => {
  const menu = findMenuByPath(routeInfo.path, menuList)
  return menu?.id
})
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

function changePath(path: string) {
  route.push(path)
}
</script>
<style lang="less" scoped>
.main-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    height: 28px;
    padding: 12px 10px 8px 10px;
    display: flex;
    align-items: center;
    overflow: hidden;

    img {
      height: 100%;
      margin: 0px 10px;
    }

    h2 {
      font-size: 16px;
      font-weight: 700;
      color: white;
      white-space: nowrap;
    }
  }
}

.el-menu {
  border-right: none;
  user-select: none;
}

.el-sub-menu {
  .el-menu-item {
    padding-left: 50px !important;
    background-color: #0c2135;
  }

  .el-menu-item:hover {
    background-color: hsl(208, 44%, 26%);
  }

  .el-menu-item.is-active {
    background-color: #0a60bd;
  }
}
</style>
